#!/usr/bin/python

"""
    This file is part of benjap.

    Copyright Graham Jones 2010.

    Benjap is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    banjap is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with benjap (COPYING file).  
    If not, see <http://www.gnu.org/licenses/>.

    $Id: geometryApp.py 9 2010-12-26 23:52:34Z grahamjones139 $
"""
"""
geometry application

"""
import os    

# Start pygame
from appBase import appBase
import pygame
from pygame.locals import QUIT, KEYDOWN, \
    K_ESCAPE, MOUSEBUTTONDOWN, MOUSEMOTION, K_q, K_F4
from random import random, randint, choice


# Make overgrown pixel map
pix_list = []
for rgb_tuple in pygame.color.THECOLORS.values():
    if sum(rgb_tuple) > 509:
        pix_surf = pygame.Surface((5, 5))
        pix_surf.fill(rgb_tuple)
        pix_list.append(pix_surf.convert())
del rgb_tuple, pix_surf

class geometryApp(appBase):

    def __init__(self,parent):
    
        print "geometryApp.__init__()"
        appBase.__init__(self,parent)

        musicFile = "sounds/popcorn.mid"

        pygame.mixer.music.load(musicFile)
        pygame.mixer.music.play()

    def onStop(self):
        print "geometryApp.onStop()"
        pygame.mixer.music.stop()

    def processEvent(self,event):
        #print "starsApp.processEvent()"
        #if event.type == MOUSEBUTTONDOWN:
        #    if event.button == 1:
        #        self.setOrigin(event.pos)
        if event.type == MOUSEMOTION:
            pass

    def processDisplay(self,delay=0):
        #Create new star - first decide where on the screen it will end up
        view_time = 1000.

        x = randint(0,self.screen_size[0])
        y = randint(0,self.screen_size[1])
        w = randint(0,self.screen_size[0]/2)
        h = randint(0,self.screen_size[1]/2)

        color = pygame.Color(
            randint(0,255),
            randint(0,255),
            randint(0,255))
        
        image = pygame.Surface((w,h))
        image.fill(color)
        
        square(x,y,view_time,image,self.Sprites)



class square(pygame.sprite.Sprite):
    def __init__(self, x, y, lifetime, image,*groups):
        pygame.sprite.Sprite.__init__(self, *groups)
        self.x = x
        self.y = y
        self.lifetime = lifetime
        self.image = image
        self.rect = self.image.get_rect(center=(int(x), int(y)))
        self.total_time = 0



    def update(self, act_delay):
        if self.total_time < self.lifetime:
            self.total_time += act_delay
        else:
            self.kill()

